import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {ApiRes} from '../common/model/response';
import {HttpClient, HttpParams} from '@angular/common/http';
import {UrlService} from '../common/service/url.service';
import {map} from 'rxjs/operators';
import _ from 'lodash';

@Injectable({
  providedIn: 'root'
})
export class PolicyAnalysisCloudService {

  constructor(private http: HttpClient,
              private urlService: UrlService) {
  }

  // 获取指定词表框架的二级词表信息
  getWordFrameTwoLevel(topId: number): Observable<ApiRes> {
    const params = new HttpParams().append('id', topId.toString());
    const url = this.urlService.getWebUrl('/policyAnalysis/getWordFreamById');
    return this.http.get<ApiRes>(url, {params}).pipe(map(res => {
      const cloudTwoLevels = [];
      for (const resultElement of res.result.children) {
        cloudTwoLevels.push({});
        const len = cloudTwoLevels.length - 1;
        cloudTwoLevels[len].id = resultElement.id;
        cloudTwoLevels[len].name = resultElement.name;
      }
      res.result = cloudTwoLevels;
      return res;
    }));
  }

  // 获取词表框架的前四个root级节点信息
  getCloud(id: number): Observable<ApiRes> {
    const params = new HttpParams().append('id', id.toString());
    const url = this.urlService.getWebUrl('/policyAnalysis/yuntu');
    return this.http.get<ApiRes>(url, {params});

  }
}
